using System.Text;
using System.Windows.Forms;
using pub.CmdUI.Cmd;
using pub.CmdUI.UI.Control;
using todo.Support;

namespace todo.Cmd.Edit.Clipboard
{
    public class PasteCmd : DefaultCmd, ICmdState
    {
        public PasteCmd()
        {
            Shortcut = new CmdShortcut(Keys.Control | Keys.V);
        }

        bool ICmdState.Enabled
        {
            get
            {
//                Debug.WriteLine("@@@ checking PasteCmd enabled...");
                var result = false;
                if (TodoApi.Instance.Ready)
                {
//                    Debug.Print("checking clipboard");
                    if (TodoApi.Instance.IsEditing() ?? false)
                    {
                        result = System.Windows.Forms.Clipboard.ContainsText();
                    }
                    else
                    {
                        result = System.Windows.Forms.Clipboard.ContainsData(TodosClip.Format);
                    }
                }
//                Debug.WriteLine("result: " + result);
                return result;
            }
        }

        protected virtual string PasteType
        {
            get { return "before"; }
        }

        public override void Execute(IControlUI controlUi)
        {
            if (TodoApi.Instance.IsEditing() ?? false)
            {
                var text = System.Windows.Forms.Clipboard.GetText();
                TodoApi.Instance.PasteText(text);
                return;
            }
            var todosClip = System.Windows.Forms.Clipboard.GetData(TodosClip.Format) as TodosClip;
            if (todosClip == null)
            {
                return;
            }
            var sb = new StringBuilder();
            Host.OutputTodos(sb, todosClip.Todos, 0);
            TodoApi.Instance.Paste(sb.ToString(), PasteType);
        }
    }
}